package g11.dao;

import g11.mis.UserTypeEnum;
import g11.model.Employee;
import java.util.List;
import java.util.ArrayList;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.apache.log4j.Logger;


public class EmployeeDAOImpl implements EmployeeDAO {
	private final Logger log = Logger.getLogger(this.getClass());

	private Session session;

	public Session getSession() {
		return this.session;
	}

	public void cleanup() {
		if (session.isOpen())
			session.close();
		// HibernateUtil.shutdown();
	}

	public EmployeeDAOImpl() {
		// log.info(getClass().toString());

	}

	public Integer createEmployee(Employee employee) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		Integer employeeId;
		try {
			tran = session.beginTransaction();
			employeeId = (Integer) session.save(employee);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return employeeId;
	}

	public void updateEmployee(Employee employee) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();
			session.update(employee);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}

	}

	public void deleteEmployee(Employee employee) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Transaction tran = null;
		try {
			tran = session.beginTransaction();

			session.delete(employee);
			tran.commit();
		} catch (HibernateException e) {
			tran.rollback();
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
	}

	public Employee getEmployeeByUsername(String username) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Employee employee = null;
		try {
			//
			String hql = "select e from Employee as e where e.username=?";
			Query q = session.createQuery(hql);
			q.setString(0, username);
			System.out.print(q.getQueryString());
			if (q.list().size() > 0) {
				employee = (Employee) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return employee;

	}

	@Override
	public Employee getEmployeeById(int employeeId) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		Employee employee = null;
		try {
			//
			String hql = "select e from Employee as e where e.id=?";
			Query q = session.createQuery(hql);
			q.setString(0, String.valueOf(employeeId));
			System.out.print(q.getQueryString());
			if (q.list().size() > 0) {
				employee = (Employee) q.list().get(0);
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return employee;
	}

	/**
	 * @author Fern
	 * this method gets same type of Employees from employee table
	 */
	public List<Employee> getAllEmployee(String employeeType) throws DAOException {
		this.session = HibernateUtil.getSessionFactory().openSession();
		List<Employee> emplist = new ArrayList<Employee>();

		/*get the employee Type*/
		byte eType=(byte) UserTypeEnum.valueOf(employeeType.toUpperCase()).index();

		try {
			/*
			 * query table Employee by type==etype;
			 */
			String hql = "select e from Employee e where e.type=?";
			Query q = session.createQuery(hql);
			q.setByte(0, eType);
			log.info(q.getQueryString());
			int qsize = q.list().size();
			if (qsize > 0) {
				for (int index = 0; index < qsize; index++) {
					Employee temp=(Employee) q.list().get(index);
					/* get the element from the list then append it to the result list*/
					emplist.add(temp);
				}
			}
		} catch (HibernateException e) {
			throw new DAOException(e.getMessage());
		} finally {
			if (session.isOpen())
				session.close();
		}
		return emplist;
	}
	
}// end of class
